//
//  AutoCell.m
//  AndonAutoCell
//
//  Created by 董文博 on 15/1/22.
//  Copyright (c) 2015年 OA. All rights reserved.
//

#import "AutoCell.h"
#import <CoreText/CoreText.h>

@interface AutoCell()
@property(nonatomic,strong)NSMutableArray *tags;
//(double (^)(double time))speedFunction
@property(nonatomic,strong)void(^clicked)(CGFloat height);
//double (^randomPercent)(void)
@end

@implementation AutoCell

-(UILabel *)titleLabel
{
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.backgroundColor = [UIColor orangeColor];
    }
    return _titleLabel;
}

-(UILabel *)contentLabel
{
    if (_contentLabel == nil) {
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.numberOfLines = 2;
    }
    return _contentLabel;
}

-(UIButton *)autoBtn
{
    if (_autoBtn == nil) {
        _autoBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    }
    return _autoBtn;
}

-(CGFloat)contentWidth
{
    
    return self.frame.size.width * 4 / 5;
}

+(NSMutableArray *)firstHeightWithData:(NSArray *)dataStrs width:(CGFloat)width
{
    NSMutableArray *heights = [[NSMutableArray alloc] init];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, width, MAXFLOAT)];
    label.numberOfLines = 0;
    for (NSString *str in dataStrs) {
        label.text = str;
        NSArray *lines = [self getSeparatedLinesFromLabel:label];
        if (lines.count == 1) {
            [heights addObject:@(60)];
        }else{
            [heights addObject:@(80)];
        }
    }
    return heights;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.titleLabel.font = [UIFont systemFontOfSize:13];
        self.contentLabel.font = [UIFont systemFontOfSize:13];
        [self.autoBtn setTitle:@"展开" forState:(UIControlStateNormal)];
        [self addSubview:self.titleLabel];
        [self addSubview:self.contentLabel];
        [self addSubview:self.autoBtn];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

+(NSArray *)getSeparatedLinesFromLabel:(UILabel *)label
{
    NSString *text = [label text];
    UIFont   *font = [label font];
    CGRect    rect = [label frame];
    
    CTFontRef myFont = CTFontCreateWithName((__bridge CFStringRef)([font fontName]), [font pointSize], NULL);
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];
    [attStr addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)myFont range:NSMakeRange(0, attStr.length)];
    
    CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)attStr);
    
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, CGRectMake(0,0,rect.size.width,100000));
    
    CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, NULL);
    
    NSArray *lines = (__bridge NSArray *)CTFrameGetLines(frame);
    NSMutableArray *linesArray = [[NSMutableArray alloc]init];
    
    for (id line in lines)
    {
        CTLineRef lineRef = (__bridge CTLineRef )line;
        CFRange lineRange = CTLineGetStringRange(lineRef);
        NSRange range = NSMakeRange(lineRange.location, lineRange.length);
        
        NSString *lineString = [text substringWithRange:range];
        [linesArray addObject:lineString];
    }
    return (NSArray *)linesArray;
}

-(NSArray *)getSeparatedLinesFromLabel:(UILabel *)label
{
    NSString *text = [label text];
    UIFont   *font = [label font];
    CGRect    rect = [label frame];
    
    CTFontRef myFont = CTFontCreateWithName((__bridge CFStringRef)([font fontName]), [font pointSize], NULL);
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];
    [attStr addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)myFont range:NSMakeRange(0, attStr.length)];
    
    CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)attStr);
    
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, CGRectMake(0,0,rect.size.width,100000));
    
    CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, NULL);
    
    NSArray *lines = (__bridge NSArray *)CTFrameGetLines(frame);
    NSMutableArray *linesArray = [[NSMutableArray alloc]init];
    
    for (id line in lines)
    {
        CTLineRef lineRef = (__bridge CTLineRef )line;
        CFRange lineRange = CTLineGetStringRange(lineRef);
        NSRange range = NSMakeRange(lineRange.location, lineRange.length);
        
        NSString *lineString = [text substringWithRange:range];
        [linesArray addObject:lineString];
    }
    return (NSArray *)linesArray;
}

-(void)zhaiKaiOnClick:(UIButton *)btn
{
    CGSize size = [self.contentLabel.text sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:CGSizeMake(self.contentWidth, MAXFLOAT) lineBreakMode:(NSLineBreakByTruncatingMiddle)];
    CGFloat height = 80;
    if (btn.selected) {
        [btn setTitle:@"展开" forState:UIControlStateNormal];
        btn.selected = NO;
        [self.tags removeObject:@(btn.tag)];
        height = 80;
    }else{
        [btn setTitle:@"收起" forState:(UIControlStateNormal)];
        btn.selected = YES;
        [self.tags addObject:@(btn.tag)];
        height = size.height+40;
    }
    self.clicked(height);
}

-(void)autoStartWith:(NSIndexPath *)indexPath tags:(NSMutableArray *)tags cellHeightClicked:(void (^)(CGFloat height))clicked
{
    self.clicked = clicked;
    self.tags = tags;
    self.titleLabel.frame = CGRectMake(0, 0, self.superview.frame.size.width, 20);
    self.contentLabel.frame = CGRectMake(10, 30, self.contentWidth, MAXFLOAT);
    [self.autoBtn setTintColor:[UIColor whiteColor]];
    [self.autoBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.autoBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateSelected)];
    
    self.autoBtn.tag = indexPath.row;
    
    if ([tags containsObject:@(self.autoBtn.tag)]) {
        self.autoBtn.selected = YES;
        [self.autoBtn setTitle:@"收起" forState:(UIControlStateNormal)];
    }else{
        self.autoBtn.selected = NO;
        [self.autoBtn setTitle:@"展开" forState:(UIControlStateNormal)];
    }
    [self.autoBtn addTarget:self action:@selector(zhaiKaiOnClick:) forControlEvents:(UIControlEventTouchUpInside)];
    
    NSArray *lines = [self getSeparatedLinesFromLabel:self.contentLabel];
    if (lines.count>=3) {
        self.autoBtn.hidden = NO;
    }else{
        self.autoBtn.hidden = YES;
    }
    
    CGSize size = [self.contentLabel.text sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:CGSizeMake(self.contentWidth, MAXFLOAT) lineBreakMode:(NSLineBreakByTruncatingMiddle)];
    if (lines.count==1) {
        if ([tags containsObject:@(self.autoBtn.tag)]) {
            self.contentLabel.numberOfLines = 0;
            self.contentLabel.frame = CGRectMake(10, 30, self.contentWidth, size.height);
        }else{
            self.contentLabel.frame = CGRectMake(10,30,self.contentWidth,20);
        }
    }else{
        if ([tags containsObject:@(self.autoBtn.tag)]) {
            self.contentLabel.numberOfLines = 0;
            self.contentLabel.frame = CGRectMake(10, 30, self.contentWidth, size.height);
        }else{
            self.contentLabel.frame = CGRectMake(10,30,self.contentWidth,40);
        }
    }
    self.autoBtn.frame = CGRectMake(CGRectGetMaxX(self.frame)-50, CGRectGetMaxY(self.contentLabel.frame)-20, 30, 20);
}

-(void)layoutSubviews
{
    [super layoutSubviews];
}

@end
